package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.User;
import com.qfedu.entity.Week;
import com.qfedu.service.WeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/week")
public class WeekController {

    @Autowired
    private WeekService weekService;

    @RequestMapping("/queryWeek.do")
    public ResponseData queryWeek(int page,int limit) {
        ResponseData<Object> responseData = new ResponseData<>();

        // 设置分页
        PageHelper.startPage(page,limit);

        List<Week> weekList = weekService.queryWeek();

        PageInfo<Week> pageInfo = new PageInfo<>(weekList);
        int total = (int) pageInfo.getTotal();

        responseData.setCode(0);
        responseData.setCount(total);
        responseData.setMsg("周报数据");
        responseData.setData(weekList);

        return responseData;
    }

    @RequestMapping("/add.do")
    public ResponseData addWeek(Week week, HttpServletRequest request) {
        // 获取用户的编号
        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");
        // 获取编号
        String no = user.getNo();

        // 设置编号到week中
        week.setU_no(no);
        week.setStatus(1);
        week.setCreateTime(new Date());

        int i = weekService.insertWeek(week);

        ResponseData<Object> responseData = new ResponseData<>();

        if (i > 0) {
            responseData.setCode(0);
            responseData.setMsg("success");
        } else {
            responseData.setCode(1);
            responseData.setMsg("fail");
        }

        return responseData;
    }

    @RequestMapping("/deleteById.do")
    public ResponseData deleteById(int id) {
        int i = weekService.deleteById(id);

        ResponseData<Object> responseData = new ResponseData<>();

        if (i > 0) {
            responseData.setCode(0);
            responseData.setMsg("success");
        } else {
            responseData.setCode(1);
            responseData.setMsg("fail");
        }

        return responseData;
    }

    @RequestMapping("/queryWeekById.do")
    public ResponseData queryWeekById(int id) {
        ResponseData<Object> responseData = new ResponseData<>();

        Week week = weekService.queryWeekById(id);

        responseData.setCode(0);
        responseData.setCount(1);
        responseData.setMsg("week信息");
        responseData.setData(week);

        return responseData;
    }

    @RequestMapping("/update.do")
    public ResponseData update(Week week, int id) {
        ResponseData responseData = new ResponseData();

        week.setId(id);
        int affectedRows = weekService.update(week);

        if (affectedRows > 0) {
            responseData.setCode(0);
            responseData.setMsg("更新成功");
        } else {
            responseData.setCode(1);
            responseData.setMsg("更新失败");
        }

        return responseData;

    }

}
